package com.vancouver.knows.ui.activity.event;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.widget.TextView;

import com.pangu.util.StringUtil;
import com.vancouver.knows.R;
import com.vancouver.knows.business.BusinessModule;
import com.vancouver.knows.business.bean.EventDetailInfo;
import com.vancouver.knows.business.bean.EventInfo;
import com.vancouver.knows.ui.base.BaseActivity;
import com.vancouver.knows.ui.fragment.event.EventCalendarListView;
import com.vancouver.knows.ui.view.compactcalendarview.CompactCalendarView;
import com.vancouver.knows.ui.view.compactcalendarview.domain.CalendarDayEvent;
import com.vancouver.knows.util.AppUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import butterknife.Bind;

public class EventCalendarActivity extends BaseActivity{

	@Bind(R.id.toobarTitle)
	TextView title;
	@Bind(R.id.toolbar)
	Toolbar toolbar;
	@Bind(R.id.compactcalendar_view)
	CompactCalendarView compactCalendarView;
	@Bind(R.id.eventListView)
	EventCalendarListView eventListView;

	private SimpleDateFormat dateFormatForMonth = new SimpleDateFormat("yyyy-MM", Locale.getDefault());
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
	private BusinessModule bModule;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_event_calendar);
		bModule = imContext.getBusinessModule();

		toolbar.setTitle("");
		setSupportActionBar(toolbar);
		toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.back));

		compactCalendarView.drawSmallIndicatorForEvents(true);
		compactCalendarView.setLocale(Locale.CHINESE);
		compactCalendarView.setUseThreeLetterAbbreviation(true);
		compactCalendarView.setCurrentDate(new Date());
		compactCalendarView.setShouldShowMondayAsFirstDay(false);

		String[] dayColumnNames = {"日","一","二","三","四","五","六",};
		compactCalendarView.setDayColumnNames(dayColumnNames);
		addEvents(compactCalendarView);
		title.setText(dateFormatForMonth.format(compactCalendarView.getFirstDayOfCurrentMonth()));
		compactCalendarView.setListener(new CompactCalendarView.CompactCalendarViewListener() {
			@Override
			public void onDayClick(Date dateClicked) {
				eventListView.setItems(getEventInfoByDate(AppUtils.getPdg(dateClicked)));
			}

			@Override
			public void onMonthScroll(Date firstDayOfNewMonth) {
				title.setText(dateFormatForMonth.format(firstDayOfNewMonth));
			}
		});

		eventListView.setItems(getEventInfoByDate(AppUtils.getPdg()));
		eventListView.initLoadableView();
	}

	private List<EventInfo> getEventInfoByDate(String date){
		return bModule.getCacheManager().getDateInfoHashMap().get(date);
	}

	private void addEvents(CompactCalendarView compactCalendarView) {
		Calendar currentCalender = Calendar.getInstance(Locale.getDefault());
		currentCalender.setTime(new Date());
		currentCalender.set(Calendar.DAY_OF_MONTH, 1);
		List<String> allDates = bModule.getCacheManager().getAllDates();
		int color = getResources().getColor(R.color.event_color);
		for (String str: allDates) {
			Date date = null;
			try {
				date = sdf.parse(str);
			} catch (ParseException e) {
				e.printStackTrace();
			}
			currentCalender.setTime(date);
			setToMidnight(currentCalender);
			compactCalendarView.addEvent(new CalendarDayEvent(currentCalender.getTimeInMillis(), color), false);
		}
		compactCalendarView.invalidate();
	}

	private void setToMidnight(Calendar calendar) {
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == android.R.id.home) {
			finish();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

}
